
import * as cesium from 'cesium/Cesium'
const Cesium =cesium
class FlowWall {
  constructor(mapControl) {
    this.mapControl = mapControl;
    this.dataList = [];
    this.install();
  }
  install() {
    //  this.uninstall();
    /*
            流动纹理线
             color 颜色
             duration 持续时间 毫秒
          */
    function PolylineTrailWallMaterialProperty(color, duration) {
      this._definitionChanged = new Cesium.Event();
      this._color = undefined;
      this._colorSubscription = undefined;
      this.color = color;
      this.duration = duration;
      this._time = (new Date()).getTime();
    }

    Object.defineProperties(PolylineTrailWallMaterialProperty.prototype, {
      isConstant: {
        get: function () {
          return false;
        }
      },
      definitionChanged: {
        get: function () {
          return this._definitionChanged;
        }
      },
      color: Cesium.createPropertyDescriptor('color')
    });
    PolylineTrailWallMaterialProperty.prototype.getType = function (time) {
      return 'PolylineTrailLink';
    }
    PolylineTrailWallMaterialProperty.prototype.getValue = function (time, result) {
      if (!Cesium.defined(result)) {
        result = {};
      }
      result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
      result.image = Cesium.Material.PolylineTrailLinkImage;
      result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
      return result;
    }
    Cesium.PolylineTrailWallMaterialProperty = PolylineTrailWallMaterialProperty;
    Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink';
    Cesium.Material.PolylineTrailLinkImage = "static/images/color3.png";
    Cesium.Material.PolylineTrailLinkSource = `czm_material czm_getMaterial(czm_materialInput materialInput)\n\
            {\n\
                czm_material material = czm_getDefaultMaterial(materialInput);\n\
                vec2 st = materialInput.st;\n\
                vec4 colorImage = texture2D(image, vec2(fract(st.t - time), st.s));\n\
                material.alpha = colorImage.a * color.a;\n\
                material.diffuse = (colorImage.rgb+color.rgb)/1.0;\n\
                return material;\n\
            }`;
    Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, {
      fabric: {
        type: Cesium.Material.PolylineTrailLinkType,
        uniforms: {
          color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
          image: Cesium.Material.PolylineTrailLinkImage,
          time: 0
        },
        source: Cesium.Material.PolylineTrailLinkSource
      },
      translucent: function (material) {
        return true;
      }
    });
  }
  addWall(list) {
    let viewer = this.mapControl.viewer;
    let maxHeight = 5000;
    let minHeight = 0;
    let centerHeight = 10;
    let posList = [];
    let maxHeightList = [];
    let minHeightList = [];
    let centerHeightList = [];
    list.forEach(item => {
      posList = [...posList, ...item];
      maxHeightList.push(maxHeight);
      minHeightList.push(minHeight);
      centerHeightList.push(centerHeight);
    })
    let wallTop = viewer.entities.add({
      wall: {
        positions: Cesium.Cartesian3.fromDegreesArray(posList),
        maximumHeights: maxHeightList,
        minimumHeights: centerHeightList,
        material: new Cesium.PolylineTrailWallMaterialProperty(Cesium.Color.BLUE.withAlpha(.9), 1000)
      }
    })

    // let wallButtom = viewer.entities.add({
    //   wall: {
    //     positions: Cesium.Cartesian3.fromDegreesArray(posList),
    //     maximumHeights: centerHeightList,
    //     minimumHeights: minHeightList,
    //     material: Cesium.Color.YELLOW.withAlpha(.7),
    //   }
    // })
    this.dataList.push({
      wallTop,
      // wallButtom
    })
  }
  removeAllWall() {
    let viewer = this.mapControl.viewer;
    this.dataList.forEach(item=>{
      if (item.wallTop) {
        viewer.entities.remove(item.wallTop);
      }
      if (item.wallTop) {
        viewer.entities.remove(item.wallButtom);
      }
    })
    this.dataList = [];
  }
  uninstall() {

  }
}
export default FlowWall;